﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GestionRecaudos
{
    public partial class FrmObligacion : Form
    {
        private bool tempSwitch = false;
        public FrmObligacion()
        {
            InitializeComponent();
        }

        private void FrmObligacion_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'gestionRecaudosDataSet.spObligacionTipo' Puede moverla o quitarla según sea necesario.
            this.spObligacionTipoTableAdapter.Fill(this.gestionRecaudosDataSet.spObligacionTipo);
            // TODO: esta línea de código carga datos en la tabla 'gestionRecaudosDataSet.spObligacion' Puede moverla o quitarla según sea necesario.
            this.spObligacionTableAdapter.Fill(this.gestionRecaudosDataSet.spObligacion);

            //-- Agrego un nuevo registro de entrada,sino hay datos
            if (this.gestionRecaudosDataSet.spObligacion.Rows.Count == 0)
            {
                this.spObligacionBindingSource.AddNew();
            }
        }

        #region Eventos asociados a los botones del BindingNavigator

        private void nuevoToolStripButtom_Click(object sender, EventArgs e)
        {
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;
            try
            {
                bs.AddNew();
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
            }
        }

        private void eliminarToolStripButtom_Click(object sender, EventArgs e)
        {
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;

            if (bs.Count == 0)
            {
                return;
            }
            if (MessageBox.Show("¿Desea Borrar el registro?", "Confirmacion", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            try
            {

                bs.RemoveCurrent();
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
            }
        }

        private void guardarToolStripButtom_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;
            try
            {
                Validate();
                bs.RemoveSort();
                bs.EndEdit();
                //---actualizar la lista
                this.tableAdapterManager.UpdateAll(this.gestionRecaudosDataSet);
                MessageBox.Show("Registros Grabados con Exito!");
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
            }
            this.Cursor = Cursors.Default;
        }

        private void deshacerToolStripButtom_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;
            try
            {
                foreach (DataTable dt in this.gestionRecaudosDataSet.Tables)
                {
                    if (bs.DataMember.ToString().Contains(dt.TableName))
                    {
                        dt.RejectChanges();
                        bs.CancelEdit();
                        //--- Remuevo el filtro
                        bs.RemoveFilter();
                        this.FiltrarToolStripTextBox.Text = string.Empty;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                bs.CancelEdit();
            }
            Cursor.Current = Cursors.Default;
        }

        private void FiltrarToolStripTextBox_TextChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripTextBox)sender).GetCurrentParent()).BindingSource;
            DataTable table = ((DataSet)bs.DataSource).Tables[bs.DataMember];
            ToolStripTextBox tb = (ToolStripTextBox)sender;
            try
            {
                String filtro = "";
                foreach (DataColumn cl in table.Columns)
                {
                    filtro += "ISNULL(" + cl.ColumnName.ToString() + ",'')+' '+";
                }
                filtro = filtro.Substring(0, filtro.Length - 5);
                filtro += " like'%" + tb.Text + "%'";
                bs.Filter = filtro;
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                bs.CancelEdit();
            }
            Cursor.Current = Cursors.Default;
        }

        private void limpiarFiltroToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;
            try
            {
                bs.RemoveFilter();
                this.FiltrarToolStripTextBox.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                bs.CancelEdit();
            }
            Cursor.Current = Cursors.Default;
        }

        private void actualizarToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ManageForms.reloadForm(this);
            Cursor.Current = Cursors.Default;
        }

        private void exportarToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try { Utils.get().exportar(this.spObligacionDataGridView); }
            catch (Exception ex) { MessageException.show(ex); }
            Cursor.Current = Cursors.Default;
        }

        #endregion

        private void porcentajeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!this.porcentajeTextBox.Text.Trim().Equals(string.Empty) && !tempSwitch)
            {
                tempSwitch = true;
                this.valorTextBox.Text = Utils.get().getFormatoMoneda(0);
                tempSwitch = false;
            }
        }

        private void valorTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!this.valorTextBox.Text.Trim().Equals(string.Empty) && !tempSwitch)
            {
                tempSwitch = true;
                this.porcentajeTextBox.Text = Utils.get().getFormatoMoneda(0);
                tempSwitch = false;
            }
        }


    }
}
